package com.xyzwps.server.paimon.ui.mvc

import com.xyzwps.server.paimon.app.ArticleAppService
import com.xyzwps.server.paimon.app.ContextUser
import com.xyzwps.server.paimon.app.dto.ArticleCreateInput
import com.xyzwps.server.paimon.app.dto.ArticlesGetInput
import com.xyzwps.server.paimon.app.dto.ArticleUpdateInput
import com.xyzwps.server.paimon.common.CursorPage
import com.xyzwps.server.paimon.spring.AuthUser
import com.xyzwps.server.paimon.ui.HttpResponse
import com.xyzwps.server.paimon.ui.view.ArticleView
import com.xyzwps.server.paimon.ui.viewservice.ArticleViewService
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.*

@Tag(name = "Articles")
@RestController
@RequestMapping(Apis.BASE + "/articles")
class ArticleController(
    private val articleApp: ArticleAppService,
    private val articleViewService: ArticleViewService
) {

    @GetMapping("/{uid}")
    fun get(@PathVariable uid: String): HttpResponse<ArticleView> {
        val article = articleApp.get(uid)
        return HttpResponse(articleViewService.inject(article))
    }

    @GetMapping
    fun get(
        @AuthUser user: ContextUser,
        input: ArticlesGetInput
    ): HttpResponse<CursorPage<ArticleView, String?>> {
        val page = articleApp.get(user, input).transfer(articleViewService::inject)
        return HttpResponse(page)
    }

    @PostMapping
    fun create(
        @AuthUser user: ContextUser,
        @RequestBody input: ArticleCreateInput
    ): HttpResponse<String> {
        return HttpResponse(articleApp.create(user, input))
    }

    @PutMapping("/{uid}")
    fun update(
        @AuthUser user: ContextUser,
        @PathVariable uid: String,
        @RequestBody input: ArticleUpdateInput
    ): HttpResponse<String> {
        return HttpResponse(articleApp.update(user, uid, input))
    }
}
